package controllers

import (
	"LoveU/utils"
	"time"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
	"github.com/astaxie/beego/session"
)

var globalSessions *session.Manager
var userSession session.Store
var SessionStore = make(map[string]session.Store)
var SessionStoreAdmin = make(map[string]session.Store)

//初始化全局的session
func init() {
	manageConfig := &session.ManagerConfig{
		CookieName: "gosessionid",
		Gclifetime: 3600000,
		// Secure:     true,
		// EnableSetCookie: true,
		// Maxlifetime:     3600,
		CookieLifeTime: 3600000,
		// ProviderConfig:  "",
		// DisableHTTPOnly: true,
		SessionIDLength: 20,
	}
	globalSessions, _ = session.NewManager("memory", manageConfig)
	// userSession, _ = globalSessions.SessionStart(ctx.ResponseWriter, ctx.Request)
	go globalSessions.GC()

}

// 检查用户的登录状态
func checkSession(ctx *context.Context, sessionid string) int64 {
	sid := ctx.Input.Session("sessionid")
	if sid == nil {
		return utils.NOT_LOGGED_IN
	} else if sid.(string) == sessionid {
		// 比对用户提交的sessionid和系统保存的是否一致
		return utils.LOGGED_IN
	}
	return utils.NOT_LOGGED_IN
}

// 获取session中的用户数据
// utils.LOGGED_IN：处于登录状态
// utils.NOT_LOGGED_IN：不处于登录状态
func getSession(ctx *context.Context) (string, string, int64) {
	name := userSession.Get("name").(string)
	if name == "" {
		return "", "", utils.NOT_LOGGED_IN
	}
	passwd := userSession.Get("passwd").(string)
	if passwd == "" {
		return "", "", utils.NOT_LOGGED_IN
	}
	return name, passwd, utils.LOGGED_IN
}

// 将sessionid写入
func writeSession(ctx *context.Context, sessionid string) int64 {

	userSession, _ = globalSessions.SessionStart(ctx.ResponseWriter, ctx.Request)
	err := userSession.Set("sessionid", sessionid)
	if err != nil {
		return 502
	}
	return 503
}

func write2Session(ctx *context.Context, name, passwd, sessionid string) int64 {
	userSession, _ = globalSessions.SessionStart(ctx.ResponseWriter, ctx.Request)
	err := userSession.Set("sessionid", sessionid)
	ctx.Input.CruSession.Set("sessionid", sessionid)
	beego.Error(ctx.Input.CruSession.Get("sessionid"))
	if err != nil {
		return 502
	}
	err = userSession.Set("name", name)
	if err != nil {
		return 502
	}
	err = userSession.Set("passwd", passwd)
	if err != nil {
		return 502
	}
	return 503
}

//将字符串转换成time.Time类型
func String2Time(date string) (time.Time, error) {
	timeLayout := "2006-01-02"                                  //转化所需模板
	loc, _ := time.LoadLocation("UTC")                          //重要：获取时区
	theTime, err := time.ParseInLocation(timeLayout, date, loc) //使用模板在对应时区转化为time.time类型
	beego.Error(theTime)
	return theTime, err
}

//过滤器
var FilterUser = func(ctx *context.Context) {
	sid_interface := ctx.Input.Session("sessionid")
	if sid_interface == nil {
		ctx.Redirect(302, "/checklogin")
	}
}

func getJSON(this *beego.Controller, status int64, msg, sid string, result interface{}) {
	codejson := utils.Message{status, msg, result, sid}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

// func notLogin(this *beego.Controller) {
// 	codejson := utils.Message{utils.NOT_LOGGED_IN, "用户未登录", nil, ""}
// 	this.Data["json"] = codejson
// 	this.ServeJSON()
// 	return
// }
func notLoginMood(this *MoodController) {
	codejson := utils.Message{utils.NOT_LOGGED_IN, "用户未登录", nil, ""}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

func notLoginFile(this *FileController) {
	codejson := utils.Message{utils.NOT_LOGGED_IN, "用户未登录", nil, ""}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

func notLoginFriend(this *FriendController) {
	codejson := utils.Message{utils.NOT_LOGGED_IN, "用户未登录", nil, ""}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

func notLoginUser(this *Usercontroller) {
	codejson := utils.Message{utils.NOT_LOGGED_IN, "用户未登录", nil, ""}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

func notLoginSearch(this *SearchController) {
	codejson := utils.Message{utils.NOT_LOGGED_IN, "用户未登录", nil, ""}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

func notLoginOther(this *OtherController) {
	codejson := utils.Message{utils.NOT_LOGGED_IN, "用户未登录", nil, ""}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

func notLoginAdmin(this *AdminController) {
	codejson := utils.Message{utils.NOT_LOGGED_IN, "用户未登录", nil, ""}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

func notLoginActivity(this *ActivityController) {
	codejson := utils.Message{utils.NOT_LOGGED_IN, "用户未登录", nil, ""}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

func getJSONMood(this *MoodController, status int64, msg, sid string, result interface{}) {
	codejson := utils.Message{status, msg, result, sid}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

func getJSONUser(this *Usercontroller, status int64, msg, sid string, result interface{}) {
	codejson := utils.Message{status, msg, result, sid}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

func getJSONFriend(this *FriendController, status int64, msg, sid string, result interface{}) {
	codejson := utils.Message{status, msg, result, sid}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

func getJSONAdmin(this *AdminController, status int64, msg, sid string, result interface{}) {
	codejson := utils.Message{status, msg, result, sid}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

func getJSONSearch(this *SearchController, status int64, msg, sid string, result interface{}) {
	codejson := utils.Message{status, msg, result, sid}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

func getJSONActivity(this *ActivityController, status int64, msg, sid string, result interface{}) {
	codejson := utils.Message{status, msg, result, sid}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

func getJSONFile(this *FileController, status int64, msg, sid string, result interface{}) {
	codejson := utils.Message{status, msg, result, sid}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}

func getJSONOther(this *OtherController, status int64, msg, sid string, result interface{}) {
	codejson := utils.Message{status, msg, result, sid}
	this.Data["json"] = codejson
	this.ServeJSON()
	return
}
